package com.epfresh.supplier.activity.standingbook;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.epfresh.api.adapter.CommonAdapter;
import com.epfresh.api.adapter.ViewHolder;
import com.epfresh.api.entity.RequestEntitySupply;
import com.epfresh.api.entity.ResponseEntity;
import com.epfresh.api.global.BaseActivity;
import com.epfresh.api.http.OnRequestListener;
import com.epfresh.api.utils.T;
import com.epfresh.api.widget.list.LoadMoreListView;
import com.epfresh.supplier.R;
import com.epfresh.supplier.adapter.StandingBookAdapter;
import com.epfresh.supplier.entity.StandingBookDateEntity;
import com.epfresh.supplier.entity.StandingBookListEntity;
import com.epfresh.supplier.entity.TotalMoneyEntity;
import com.epfresh.supplier.widget.PullScrollView;
import com.google.gson.Gson;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by shenjie on 2017/9/19.
 */

public class StandingBookActivity extends BaseActivity implements LoadMoreListView.IXListViewListener, DatePickerPopupWindow.DatePickerListener,
        AdapterView.OnItemClickListener, PullScrollView.onRefreshListener, ListTimeDatePicker.AnimationEndListener {
    private TextView tvWait, tvGathering, tvDrawback, tvOnaccount;
    private ListView listViewResult;
    private RelativeLayout allContainer, unclearContainer, clearContainer;
    private LinearLayout imageCon;
    private TextView tvAll, tvUnclear, tvClear, cash, other;
    private View vAll, vUnclear, vClear;
    private RelativeLayout gatheringContainer, drawbackContainer, onaccountContainer, cashotherContainer;
    private DatePickerPopupWindow popupWindow;
    private int currentType = 0;
    private StandingBookAdapter adapter;
    private List<StandingBookListEntity.StandingBookList> allList = new ArrayList<>();
    private List<StandingBookListEntity.StandingBookList> unclearList = new ArrayList<>();
    private List<StandingBookListEntity.StandingBookList> clearList = new ArrayList<>();
    private int allNumber, unclearNumber, clearNumber;
    private int allTimePosition, unclearTimePosition, clearTimePosition;
    private String allString, unclearString, clearString;
    private Spanned origin;
    private DecimalFormat df1;
    private TextView allhint;
    private ImageView unclearHint;
    private PullScrollView test;
    private ListTimeDatePicker listTimeDatePicker;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        allNumber = unclearNumber = clearNumber = 0;
        allTimePosition = unclearTimePosition = clearTimePosition = -1;
        origin = Html.fromHtml("<big>" + "0" + "</big>" + "<small>." + "0" + "</small>");
        initView();
        df1 = (DecimalFormat) DecimalFormat.getInstance();
        df1.setGroupingSize(3);

    }

    private void initView() {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        actionbar.findViewById(R.id.v_line).setVisibility(View.GONE);
        String date = sdf.format(new Date());
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setImageResource(R.mipmap.title_left_white);
        tvBack.setVisibility(View.VISIBLE);
        tvBack.setTextColor(getResources().getColor(R.color.white));
        actionbar.setBackgroundColor(getResources().getColor(R.color.yellow_little));
        currentStr=allString = date;
        toolbarTitle.setText(date + "台账");
        Drawable right = getResources().getDrawable(R.mipmap.dropdown);
        right.setBounds(0, 0, right.getMinimumWidth(), right.getMinimumHeight());
        toolbarTitle.setCompoundDrawables(null, null, right, null);
        toolbarTitle.setOnClickListener(this);
        toolbarTitle.setTextColor(getResources().getColor(R.color.white));
        righticon.setImageResource(R.mipmap.white_calender);
        righticon.setVisibility(View.VISIBLE);
        righticon.setOnClickListener(this);
        setContentView(R.layout.activity_standingbook);
        imageCon = (LinearLayout) findViewById(R.id.imagecon);
        tvWait = (TextView) findViewById(R.id.tv_sum_new);
        tvGathering = (TextView) findViewById(R.id.tv_gathering);
        tvDrawback = (TextView) findViewById(R.id.tv_drawback);
        tvOnaccount = (TextView) findViewById(R.id.tv_onaccount);
        listViewResult = (ListView) findViewById(R.id.result);
        allContainer = (RelativeLayout) findViewById(R.id.all_container);
        allContainer.setOnClickListener(this);
        unclearContainer = (RelativeLayout) findViewById(R.id.unclear_container);
        unclearContainer.setOnClickListener(this);
        clearContainer = (RelativeLayout) findViewById(R.id.clear_container);
        clearContainer.setOnClickListener(this);
        tvAll = (TextView) findViewById(R.id.all_tv);
        tvUnclear = (TextView) findViewById(R.id.unclear_tv);
        tvClear = (TextView) findViewById(R.id.clear_tv);
        vAll = findViewById(R.id.all_v);
        vUnclear = findViewById(R.id.unclear_v);
        vClear = findViewById(R.id.clear_v);
        gatheringContainer = (RelativeLayout) findViewById(R.id.gathering_container);
        gatheringContainer.setOnClickListener(this);
        cashotherContainer = (RelativeLayout) findViewById(R.id.cashothercontainer);
        cashotherContainer.setOnClickListener(this);
        cash = (TextView) findViewById(R.id.cash);
        other = (TextView) findViewById(R.id.other);
        drawbackContainer = (RelativeLayout) findViewById(R.id.drawback_container);
        onaccountContainer = (RelativeLayout) findViewById(R.id.onaccount_container);
        onaccountContainer.setOnClickListener(this);
        adapter = new StandingBookAdapter(new ArrayList<StandingBookListEntity.StandingBookList>(), this);
        listViewResult.setAdapter(adapter);
        allContainer.performClick();
        loadDateHttp();
        loadTotalMoney();
        loadstandbookHttp();
        listViewResult.setOnItemClickListener(this);
        test = (PullScrollView) findViewById(R.id.test);
        test.setOnRefreshListener(this);

        tvWait.setText(origin);
        tvGathering.setText(origin);
        tvDrawback.setText(origin);
        tvOnaccount.setText(origin);

        allhint = (TextView) findViewById(R.id.all_hint);
        unclearHint = (ImageView) findViewById(R.id.unclear_hint);
    }


    @Override
    public void onClick(View view) {
        super.onClick(view);
        switch (view.getId()) {
            case R.id.all_container:
                setSegment(0);
                break;
            case R.id.clear_container:
                setSegment(2);
                break;
            case R.id.unclear_container:
                setSegment(1);
                break;
            case R.id.rl_time:
            case R.id.righticon:
                if (popupWindow != null && popupWindow.isShowing()) {
                    popupWindow.setNeed(false);
                    popupWindow.viewOut();
                }
                if (listTimeDatePicker.isShowing()) {
                    listTimeDatePicker.setNeed(false);
                    listTimeDatePicker.viewOut();
                } else {
                    listTimeDatePicker.showAsDropDown(toolbarTitle);
                    listTimeDatePicker.viewIn();
                }
                break;

            case R.id.toolbar_title:
                if (listTimeDatePicker != null && listTimeDatePicker.isShowing()) {
                    listTimeDatePicker.setNeed(false);
                    listTimeDatePicker.dismiss();
                }
                if (popupWindow == null) {
                    popupWindow = new DatePickerPopupWindow(this);
                    popupWindow.setListener(this);
                }
                if (popupWindow.isShowing()) {
                    popupWindow.setNeed(false);
                    popupWindow.viewOut();
                } else {
                    popupWindow.showAsDropDown(toolbarTitle, 0, -actionbar.getHeight());
                    popupWindow.viewIn();
                }
                break;
            case R.id.gathering_container:
                if (cashotherContainer.getVisibility() == View.GONE) {
                    cashotherContainer.setVisibility(View.VISIBLE);
                } else {
                    cashotherContainer.setVisibility(View.GONE);
                }
                break;
            case R.id.cashothercontainer:
                cashotherContainer.setVisibility(View.GONE);
                break;
            case R.id.onaccount_container:
                Intent intent = new Intent(this, DebtActivity.class);
                intent.putExtra("queryDate",currentStr);
                Log.e("queryDate",""+currentStr);
                startActivityForResult(intent, 100);
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (data != null && data.getBooleanExtra("hasCancel", false)) {
            loadTotalMoney();
        }
    }

    private void setSegment(int select) {
        currentType = select;
        toolbarTitle.setText(currentStr);
        if (select == 0) {
            tvAll.setTextColor(getResources().getColor(R.color.red));
            vAll.setVisibility(View.VISIBLE);
            tvClear.setTextColor(getResources().getColor(R.color.wholesale));
            vClear.setVisibility(View.INVISIBLE);
            tvUnclear.setTextColor(getResources().getColor(R.color.wholesale));
            vUnclear.setVisibility(View.INVISIBLE);
            adapter.setLists(allList);
            if (!currentStr.equals(allString)) {
                allString=currentStr;
                allNumber = 0;
                allList.clear();
                loadstandbookHttp();
                loadTotalMoney();
            }else {
                if (allList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }
                adapter.notifyDataSetChanged();
            }
        } else if (select == 2) {
            tvAll.setTextColor(getResources().getColor(R.color.wholesale));
            vAll.setVisibility(View.INVISIBLE);
            tvClear.setTextColor(getResources().getColor(R.color.red));
            vClear.setVisibility(View.VISIBLE);
            tvUnclear.setTextColor(getResources().getColor(R.color.wholesale));
            vUnclear.setVisibility(View.INVISIBLE);
            adapter.setLists(clearList);
            if (!currentStr.equals(clearString)) {
                clearString=currentStr;
                clearNumber=0;
                clearList.clear();
                loadstandbookHttp();
                loadTotalMoney();
            }else {
                if (clearList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }
                adapter.notifyDataSetChanged();
            }
        } else {
            tvAll.setTextColor(getResources().getColor(R.color.wholesale));
            vAll.setVisibility(View.INVISIBLE);
            tvClear.setTextColor(getResources().getColor(R.color.wholesale));
            vClear.setVisibility(View.INVISIBLE);
            tvUnclear.setTextColor(getResources().getColor(R.color.red));
            vUnclear.setVisibility(View.VISIBLE);
            adapter.setLists(unclearList);
            if (!currentStr.equals(unclearString)) {
                unclearString=currentStr;
                unclearNumber=0;
                unclearList.clear();
                loadstandbookHttp();
                loadTotalMoney();
            }else {
                if (unclearList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }
                adapter.notifyDataSetChanged();
            }
        }
        toolbarTitle.setText(allString + "台账");
        adapter.notifyDataSetChanged();
    }

    private void loadDateHttp() {
        RequestEntitySupply<Map<String, Object>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_queryStoreOrderTime);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_queryStoreOrderTime, onRequestListener);
    }

    private void loadTotalMoney() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_findStoreTotalFee);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> maps = new HashMap<>();
        maps.put("storeId", getAppApplication().getStoreId());
//        maps.put("storeId","66");
        maps.put("queryDate", currentStr);

//        maps.put("queryDate","2017-09-20");
        maps.put("type", 0 + "");
        data.setParameters(maps);
        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_findStoreTotalFee, onTotalMoneyRequestListener);
    }

    private OnRequestListener<TotalMoneyEntity> onTotalMoneyRequestListener = new OnRequestListener<TotalMoneyEntity>() {
        @Override
        public TotalMoneyEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, TotalMoneyEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            T.toast("获取金额失败");

        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            T.toast("获取金额失败");
        }

        @Override
        public void onStart(Object tag) {

        }

        @Override
        public void onResponse(ResponseEntity<TotalMoneyEntity> response, Object tag) {
            TotalMoneyEntity entity = response.getResponseElement();
//            tvWait.setText(entity.getReceivedFee());
            String[] receiveStr = (entity.getReceivedFee().toString()).split("\\.");
            if (receiveStr.length != 2) {
                tvWait.setText(origin);
            } else {
                Spanned ss = Html.fromHtml("<big>" + df1.format(Long.valueOf(receiveStr[0])) + "</big>" + "<small>." + receiveStr[1] + "</small>");
                tvWait.setText(ss);
            }

            String[] payStr = entity.getPayFee().toString().split("\\.");
            if (payStr.length != 2) {
                tvGathering.setText(origin);
            } else {
                Spanned ss = Html.fromHtml("<big>" + payStr[0] + "</big>" + "<small>." + payStr[1] + "</small>");
                tvGathering.setText(ss);
            }

            String[] refundStr = entity.getRefundFee().toString().split("\\.");
            if (refundStr.length != 2) {
                tvDrawback.setText(origin);
            } else {
                Spanned ss = Html.fromHtml("<big>" + refundStr[0] + "</big>" + "<small>." + refundStr[1] + "</small>");
                tvDrawback.setText(ss);
            }

            String[] debtStr = entity.getDebtFee().toString().split("\\.");
            if (debtStr.length != 2) {
                tvOnaccount.setText(origin);
            } else {
                Spanned ss = Html.fromHtml("<big>" + debtStr[0] + "</big>" + "<small>." + debtStr[1] + "</small>");
                tvOnaccount.setText(ss);
            }
            cash.setText("现金:" + entity.getOfflineCashFee() + "元");
            other.setText("支付宝/微信:" + entity.getOnlineFee() + "元");
            String time = dateFormat.format(new Date());
            test.setTvTime("最后一次刷新时间：今天" + time);

        }
    };

    SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm");


    private OnRequestListener<StandingBookDateEntity> onRequestListener = new OnRequestListener<StandingBookDateEntity>() {
        @Override
        public StandingBookDateEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StandingBookDateEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
        }

        @Override
        public void onStart(Object tag) {
            showProgressDialog();
        }

        @Override
        public void onResponse(ResponseEntity<StandingBookDateEntity> response, Object tag) {
            hideProgressDialog();
            timeList = (ArrayList<StandingBookDateEntity.DateContent>) response.getResponseElement().getContent();
            if (timeList != null && timeList.size() > 0) {
                timeAdapter = new TimeAdapter(StandingBookActivity.this, timeList, R.layout.item_string_book);
                listTimeDatePicker = new ListTimeDatePicker(StandingBookActivity.this, timeAdapter);
                listTimeDatePicker.setListener(StandingBookActivity.this);
            }
        }
    };

    private int timePosition = -1;
    private ArrayList<StandingBookDateEntity.DateContent> timeList;
    private String timeString;
    private TimeAdapter timeAdapter;

    @Override
    public void onLoadMore() {
        loadstandbookHttp();
    }

    @Override
    public void select(String date) {
        toolbarTitle.setText(date + "台账");
        if (currentType == 0) {
            if (date.equals(allString)) {
                return;
            }
        } else if (currentType == 1) {
            if (date.equals(unclearString)) {
                return;
            }
        } else {
            if (date.equals(clearString)) {
                return;
            }
        }
        hasMore = true;
        test.setTvHint("松开加载更多数据...");
        if (currentType == 0) {
            allNumber = 0;
            allList.clear();
            allString = date.toString();
        } else if (currentType == 1) {
            unclearNumber = 0;
            unclearList.clear();
            unclearString = date.toString();
        } else {
            clearNumber = 0;
            clearList.clear();
            clearString = date.toString();
        }
        currentStr = date.toString();
        loadstandbookHttp();
        loadTotalMoney();
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        StandingBookListEntity.StandingBookList entity = null;
        if (currentType == 0) {
            entity = allList.get(position);
        } else if (currentType == 1) {
            entity = unclearList.get(position);
        } else {
            entity = clearList.get(position);
        }
        if (entity != null) {
            Intent intent = new Intent(this, StandingBookDetailActivity.class);
            intent.putExtra("orderId", entity.getOrderId() + "");
            intent.putExtra("debtFee", entity.getDebtFee() + "");
            startActivity(intent);
        }
    }

    @Override
    public void refresh() {
        loadTotalMoney();
        if(currentType==0){
            allNumber=0;
            allList.clear();
        }else if(currentType==1){
            unclearNumber=0;
            unclearList.clear();
        }else {
            clearNumber=0;
            clearList.clear();
        }
        if (hasMore) {
            loadstandbookHttp();
        }

    }

    @Override
    public void pushRefresh() {
        if (hasMore) {
            loadstandbookHttp();
        }
    }

    @Override
    public void load(boolean needLoad) {
        if (needLoad) {
            loadTotalMoney();
            loadstandbookHttp();
        }
    }

    private String currentStr;

    class TimeAdapter extends CommonAdapter<StandingBookDateEntity.DateContent> implements View.OnClickListener {
        public TimeAdapter(Context context, ArrayList<StandingBookDateEntity.DateContent> runPacks, int a) {
            super(context, runPacks, a);
        }

        public void onClick(View v) {
            Object tag = v.getTag(R.id.item_key_position);
            if (tag != null && tag instanceof Integer) {
                timePosition = (int) tag;
                if (allTimePosition == (int) tag) {
                    allTimePosition = (int) tag;
                    listTimeDatePicker.setNeed(false);
                    listTimeDatePicker.viewOut();
                    return;
                }
                allTimePosition = (int) tag;
                listTimeDatePicker.setNeed(true);
                hasMore = true;
                test.setTvHint("松开加载更多数据...");
                currentStr = timeList.get(timePosition).getMonth();
                if (currentType == 0) {
                    allNumber = 0;
                    allList.clear();
                    allString = timeList.get(timePosition).getMonth();
                } else if (currentType == 1) {
                    unclearNumber = 0;
                    unclearList.clear();
                    unclearString = timeList.get(timePosition).getMonth();
                } else {
                    clearNumber = 0;
                    clearList.clear();
                    clearString = timeList.get(timePosition).getMonth();
                }

                toolbarTitle.setText(timeList.get(timePosition).getMonth() + "台账");
                listTimeDatePicker.setNeed(true);
                listTimeDatePicker.viewOut();
                adapter.notifyDataSetChanged();

//                if (timeList.get(timePosition).getMonth().equals(timeString)) {
//                    return;//如果点选的是当前时间，直接取消对话框即可
//                }
            }
        }

        @Override
        public void convert(final ViewHolder holder, final StandingBookDateEntity.DateContent bean) {
            holder.getView(R.id.tv_string).setTag(R.id.item_key_position, timeList.indexOf(bean));
            holder.setOnClickListener(R.id.tv_string, this);
            holder.setText(R.id.tv_string, bean.getStr());
            if (timeList.indexOf(bean) == allTimePosition) {
                holder.getView(R.id.v_flag).setVisibility(View.VISIBLE);
                ((TextView) holder.getView(R.id.tv_string)).setTextColor(Color.RED);
            } else {
                holder.getView(R.id.v_flag).setVisibility(View.INVISIBLE);
                ((TextView) holder.getView(R.id.tv_string)).setTextColor(Color.parseColor("#434343"));
            }
        }
    }

    private void loadstandbookHttp() {
        RequestEntitySupply<Map<String, String>> data = new RequestEntitySupply<>();
        data.setCmd(com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_findOrderInfo);
        data.setClientIdentifierCode(getAppApplication().getClientIdentifierCode());
        Map<String, String> maps = new HashMap<>();
        maps.put("storeId", getAppApplication().getStoreId());
//        maps.put("storeId",66+"");
//        maps.put("queryDate",timeString);
        if (currentType == 0) {
            maps.put("pageNumber", allNumber + "");
        } else if (currentType == 1) {
            maps.put("pageNumber", unclearNumber + "");
        } else {
            maps.put("pageNumber", clearNumber + "");
        }
        maps.put("queryDate", currentStr);
//        maps.put("queryDate","2017-09-20");
        maps.put("type", currentType + "");

        maps.put("pageSize", 10 + "");
        data.setParameters(maps);

        request(data, com.epfresh.api.constant.AppSupplyRouterConstant.cmd_finance_payment_pos_findOrderInfo, onListRequestListener);
    }

    private boolean hasMore = true;

    private OnRequestListener<StandingBookListEntity> onListRequestListener = new OnRequestListener<StandingBookListEntity>() {
        @Override
        public StandingBookListEntity jsonToObj(String responseStr) {
            return new Gson().fromJson(responseStr, StandingBookListEntity.class);
        }

        @Override
        public void onFail(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            test.stopRefresh();
        }

        @Override
        public void onResponseError(int failCode, Object arg, Object tag) {
            hideProgressDialog();
            test.stopRefresh();
        }

        @Override
        public void onStart(Object tag) {
        }

        @Override
        public void onResponse(ResponseEntity<StandingBookListEntity> response, Object tag) {
            hideProgressDialog();
            StandingBookListEntity book = response.getResponseElement();

            if (currentType == 0) {
                allNumber = book.getNumber();
            } else if (currentType == 1) {
                unclearNumber = book.getNumber();
            } else {
                clearNumber = book.getNumber();
            }
            if (!book.isLast()) {
                hasMore = true;
                test.setTvHint("松开加载更多数据...");
                if (currentType == 0) {
                    allNumber++;
                } else if (currentType == 1) {
                    unclearNumber++;
                } else {
                    clearNumber++;
                }
            } else {
                if (book.getContent().size() == 10) {
                    if (currentType == 0) {
                        allNumber++;
                    } else if (currentType == 1) {
                        unclearNumber++;
                    } else {
                        clearNumber++;
                    }
                }
                test.setTvHint("已经加载全部数据...");
            }

            if (currentType == 0) {
                if (book.getTotalElements() != null && !book.getTotalElements().equals("") && !book.getTotalElements().equals("0")) {
                    allhint.setVisibility(View.VISIBLE);
                    allhint.setText(book.getTotalElements());
                } else {
                    allhint.setVisibility(View.GONE);
                }

                for (StandingBookListEntity.StandingBookList item : book.getContent()) {
                    boolean has = false;
                    for (int i = 0; i < allList.size(); i++) {
                        StandingBookListEntity.StandingBookList item1 = allList.get(i);
                        if (item.getOrderId() == item1.getOrderId()) {
                            has = true;
                            allList.remove(item1);
                            allList.add(i, item);
                            break;
                        }
                    }
                    if (!has) {
                        allList.add(item);
                    }
                }
                boolean hasUnclear=false;
                for(StandingBookListEntity.StandingBookList item:allList){
                    if(item.getDebtFee()!=0){
                        hasUnclear=true;
                        break;
                    }
                }
                if(hasUnclear){
                    unclearHint.setVisibility(View.VISIBLE);
                }else {
                    unclearHint.setVisibility(View.GONE);
                }
                Collections.sort(allList, new Comparator<StandingBookListEntity.StandingBookList>() {
                    @Override
                    public int compare(StandingBookListEntity.StandingBookList o1, StandingBookListEntity.StandingBookList o2) {
                        return -o1.getCreateOrderTime().compareTo(o2.getCreateOrderTime());
                    }
                });
            } else if (currentType == 1) {
                for (StandingBookListEntity.StandingBookList item : book.getContent()) {
                    boolean has = false;
                    for (int i = 0; i < unclearList.size(); i++) {
                        StandingBookListEntity.StandingBookList item1 = unclearList.get(i);
                        if (item.getOrderId() == item1.getOrderId()) {
                            has = true;
                            unclearList.remove(item1);
                            unclearList.add(i, item);
                            break;
                        }
                    }
                    if (!has) {
                        unclearList.add(item);
                    }
                }
                if (unclearList.size() > 0) {
                    unclearHint.setVisibility(View.VISIBLE);
                } else {
                    unclearHint.setVisibility(View.GONE);
                }
                Collections.sort(unclearList, new Comparator<StandingBookListEntity.StandingBookList>() {
                    @Override
                    public int compare(StandingBookListEntity.StandingBookList o1, StandingBookListEntity.StandingBookList o2) {
                        return -o1.getCreateOrderTime().compareTo(o2.getCreateOrderTime());
                    }
                });
            } else {
                for (StandingBookListEntity.StandingBookList item : book.getContent()) {
                    boolean has = false;
                    for (int i = 0; i < clearList.size(); i++) {
                        StandingBookListEntity.StandingBookList item1 = clearList.get(i);
                        if (item.getOrderId() == item1.getOrderId()) {
                            has = true;
                            clearList.remove(item1);
                            clearList.add(i, item);
                            break;
                        }
                    }
                    if (!has) {
                        clearList.add(item);
                    }
                }
                Collections.sort(clearList, new Comparator<StandingBookListEntity.StandingBookList>() {
                    @Override
                    public int compare(StandingBookListEntity.StandingBookList o1, StandingBookListEntity.StandingBookList o2) {
                        return -o1.getCreateOrderTime().compareTo(o2.getCreateOrderTime());
                    }
                });
            }
            if (currentType == 0) {
                adapter.setLists(allList);
                if (allList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }

            } else if (currentType == 1) {
                if (unclearList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }
                adapter.setLists(unclearList);
            } else {
                if (clearList.size() > 0) {
                    setListAndImageVisible(0);

                } else {
                    setListAndImageVisible(1);
                }
                adapter.setLists(clearList);
            }
            adapter.notifyDataSetChanged();
            if (timeAdapter != null) {
                timeAdapter.notifyDataSetChanged();
            }
            test.stopRefresh();
        }
    };

    private void setListAndImageVisible(int type) {
        if (type == 0) {
            listViewResult.setVisibility(View.VISIBLE);
            imageCon.setVisibility(View.GONE);
        } else {
            imageCon.setVisibility(View.VISIBLE);
            listViewResult.setVisibility(View.INVISIBLE);
        }

    }


}
